package com.shen.library_nettysocket.handler

import io.netty.channel.ChannelInboundHandlerAdapter
import io.netty.channel.ChannelHandlerContext
import com.shen.library_nettysocket.netty.NettyTcpClient
import com.shen.library_nettysocket.protobuf.MessageProtobuf
import java.lang.Exception

/**
 * 心跳消息响应处理handler
 */
class HeartbeatRespHandler(private val imsClient: NettyTcpClient) : ChannelInboundHandlerAdapter() {
    @Throws(Exception::class)
    override fun channelRead(ctx: ChannelHandlerContext, msg: Any?) {

        if(msg !is MessageProtobuf.Msg?) return
        val heartbeatRespMsg: MessageProtobuf.Msg? = msg
        if (heartbeatRespMsg?.head == null) return


        val heartbeatMsg: MessageProtobuf.Msg? = imsClient.heartbeatMsg
        if (heartbeatMsg?.head == null) {
            return
        }

        when(heartbeatMsg.head.msgType){
            heartbeatRespMsg.head.msgType -> println("收到服务端心跳响应消息，message=$heartbeatRespMsg")
            else -> ctx.fireChannelRead(msg)        // 消息透传
        }
    }

}